//
//  ToolViewViewController.m
//  IWaneHome
//
//  Created by hwd on 印度历1938/3/22.
//  Copyright © 印度历1938年 hwd_zlg. All rights reserved.
//

#import "ToolViewViewController.h"

@interface ToolViewViewController ()

@end

@implementation ToolViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
//获得字符串的宽度
+(CGFloat)getWidthFromString:(NSString *)str Font:(float)font
{
    CGRect rect = [str boundingRectWithSize:CGSizeMake(220, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName, nil] context:nil];
    return rect.size.width;
}
//获取字符串的高度
+(CGFloat)getHeightFromString:(NSString *)str Font:(float)font
{
    CGRect rect = [str boundingRectWithSize:CGSizeMake(220, 1000) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName, nil] context:nil];
    return rect.size.height;
}
//导航条设置
+(void)DaoHangtiaoColor:(UIViewController *)selfControV
{
    selfControV.navigationController.navigationBar.hidden = NO;
    selfControV.navigationController.navigationBarHidden = NO;
    
    selfControV.extendedLayoutIncludesOpaqueBars = YES;
    selfControV.navigationController.navigationBar.translucent = NO;
    
    //去掉back字
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                     forBarMetrics:UIBarMetricsDefault];
    //改变导航条背景颜色
    [selfControV.navigationController.navigationBar setBarTintColor:KwBlueColor];
    //改变导航条标题栏字体颜色
    [selfControV.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //改变导航条返回键颜色
    selfControV.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}

/**
 *  左边图片按钮
 */
-(void)createLeftButtonWithImage:(UIImage *)image selector:(SEL)selector;
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:selector];
}

//弹窗
+(void)addAlterViewController:(NSString *)msg Controller:(UIViewController *)controller
{
 
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:msg preferredStyle:  UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [controller presentViewController:alert animated:true completion:nil];
}
//pop弹窗
+(void)addPopAlterViewController:(NSString *)msg Controller:(UIViewController *)controller
{

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [controller.navigationController popViewControllerAnimated:YES];
    }]];
    //弹出提示框；
    [controller presentViewController:alert animated:true completion:nil];
}
//Key弹窗
+(void)addKeyAlterViewController:(NSString *)msg Controller:(UIViewController *)controller
{

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:msg preferredStyle:  UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //点击按钮的响应事件；

    }]];
    //弹出提示框
    [controller presentViewController:alert animated:true completion:nil];
}
+(UILabel *)addLineLab:(CGRect)cg
{
    UILabel *label = [[UILabel alloc]initWithFrame:cg];
    label.backgroundColor = [UIColor colorWithHexString:@"#58Aeef"];
    return label;
}
+(UILabel *)addCellLineLab:(CGRect)cg
{
    UILabel *label = [[UILabel alloc]initWithFrame:cg];
    label.backgroundColor = [UIColor colorWithHexString:@"ececec"];
    return label;
}
+ (float) heightForTextViewWithText: (NSString *) strText{
    CGSize constraint = CGSizeMake(SCREENW-50*screenW_6, CGFLOAT_MAX);
    CGRect size = [strText boundingRectWithSize:constraint
                                        options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                     attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]}
                                        context:nil];
    float textHeight = size.size.height + 30.0;
    
    return textHeight;
}
+(BOOL)isNotNullStr:(id)DataStr
{
    if ([DataStr isEqual:@""] || [DataStr isEqual:@" "] || [DataStr isKindOfClass:[NSNull class]] || [DataStr isEqual:@"(null)"] || [DataStr isEqual:@"<nil>"] || DataStr == NULL) {
        return NO;
    }
    return YES;
}
+(id)GetDataStr:(id)DataStr
{
    if ([DataStr isEqual:@""] || [DataStr isKindOfClass:[NSNull class]] || [DataStr isEqual:@"(null)"] || [DataStr isEqual:@"<nil>"] || DataStr == NULL) {
        return @" ";
    }
    return DataStr;
}
//更新
+(void)addGengXin
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSArray *neiArr;
    if([appVersion rangeOfString:@"."].location !=NSNotFound)//是否含有.
    {
        neiArr = [appVersion componentsSeparatedByString:@"."];
    }else//不含有
    {
        neiArr = [[NSArray alloc]initWithObjects:appVersion,@"0",nil];
    }
    //appType 类型(0=商家版,1=用户版，2=师傅版)
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:[NSString stringWithFormat:@"%@/jeesite/interface/appVersion/details",baseUrlMe] Dic:@{@"type":@"2"} completion:^(NSDictionary *responseDic) {
            if ([responseDic[@"success"] boolValue]) {//请求成功
                NSString *version = responseDic[@"data"][@"varsionCode"];
                version = [version stringByReplacingOccurrencesOfString:@"v" withString:@""];
                if([version rangeOfString:@"."].location !=NSNotFound)//含有点
                {
                    NSArray *arr = [version componentsSeparatedByString:@"."];
                    if ([neiArr[0] intValue]<[arr[0] intValue]) {//第一个数小于跟新
                        [self goLoadAppUrl:responseDic];
                        return;
                    }
                    else if ([neiArr[0] intValue] ==[arr[0] intValue]) {//第一个数小于相等
                        
                        if ([neiArr[1] intValue]<[arr[1] intValue]) {//比较第二个数
                            [self goLoadAppUrl:responseDic];
                            return;
                            
                        }else if ([neiArr[1] intValue]==[arr[1] intValue] && neiArr.count>=3 && arr.count>=3) {
                            
                            if ([neiArr[2] intValue]< [arr[2] intValue]) {//前两位相等 第三个数小于跟新
                                [self goLoadAppUrl:responseDic];
                                return;
                            }
                            
                        } else if ([neiArr[1] intValue]==[arr[1] intValue] && arr.count>=3)
                        {
                            
                            if ([arr[2] intValue]>0)//前两位相等 第三位>0 更新
                            {
                                [self goLoadAppUrl:responseDic];
                                return;
                            }
                        }
                    }
                }else//不包含远点 直接比较
                {
                    if([appVersion integerValue]<[version integerValue])
                    {
                        [self goLoadAppUrl:responseDic];
                        return;
                    }
                }
            }
        }];
    });
}
+(void)goLoadAppUrl:(NSDictionary *)genXinDic//去更新
{
    NSString *url = genXinDic[@"data"][@"urls"];
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"有新版本 请更新" preferredStyle:  UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if([url rangeOfString:@"http"].location !=NSNotFound)
            {
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
            }
            else
            {
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@",url]]];
            }
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
        //弹出提示框；
        [[TomamaUI getTopVC] presentViewController:alert animated:true completion:nil];
    });
}
@end
